﻿using System;
using System.Collections.Generic;
using System.Text;
using ttlang.node;

namespace ttlang.builder
{
    static class FileExporter
    {
        public static void ExportNameSpaceMember(Result result, string name, semantic.TTNameSpace _namespace)
        {
            StringBuilder coder = new StringBuilder();
            coder.AppendLine("using System;");
            //c# 不允许namespace有member，全部用class套起来
            coder.AppendLine("static class " + name);
            coder.AppendLine("{");
            if (_namespace.methods != null)
            {
                foreach (var m in _namespace.methods.Values)
                {
                    BuildMethod(coder, 1, m);
                }
            }
            if (_namespace.vars != null)
            {
                foreach (var v in _namespace.vars.Values)
                {
                    BuildVar(coder, 1, v);
                }
            }
            coder.AppendLine("}");


            var filename = name + ".cs";
            var len = coder.Length;

            result.WriteFile(filename, coder.ToString());
            result.outputFiles[filename] = len;
        }

        public static void ExportType(TsBuilderContext option, Result result, string name, semantic.TTType _type)
        {

        }
        public static void BuildMethod(StringBuilder coder, int deep, semantic.TTMethod method)
        {

            bool isMain = false;
            if (method.attributes != null)//入口点检查
            {
                for (var i = 0; i < method.attributes.Length; i++)
                {
                    var _m = method.attributes[i].function as Node_Expr_Method;
                    if (_m.method.name == "EntryPoint")
                    {
                        isMain = true;
                    }
                }
            }
            string space = "";
            for (var i = 0; i < deep; i++)
                space += "    ";
            coder.Append(space);

            //將來加上各種modifer以後，逻辑应该是有entrypoint的必须是static函数，现在还没区分static
            //所有namespace里面的也是static函数
            if (isMain)
                 coder.Append(" static ");
            
            coder.Append( ts.TypeConverter.ConvertType(method.tttype.fullname));
            coder.Append(" " + (isMain ? "Main" : method.name) + "(");
            for (var i = 0; i < method.args.Length; i++)
            {
                coder.Append(ts.TypeConverter.ConvertType(method.args[i].argtype.fullname) + " " + method.args[i].name);
                if (i != method.args.Length - 1)
                    coder.Append(",");
            }
            coder.AppendLine(")");


            if (method.body != null)
            {
                NodeExporter.BuildNode_Code(coder, method.body, deep);
            }
            else
            {
                coder.AppendLine(space + "{");
                coder.AppendLine(space + "}");
            }
        }
        public static void BuildVar(StringBuilder coder, int space, semantic.TTVar method)
        {

        }
    }
}
